using Microsoft.AspNetCore.Mvc;
using Scheduling.Services.Weathers;
using System;
using System.Threading.Tasks;

namespace Scheduling.Api.Read.Controllers
{
    /// <summary>
    /// 天气控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class WeatherController : ControllerBase
    {
        private readonly IWeatherService _weatherService;

        public WeatherController(IWeatherService weatherService)
        {
            _weatherService = weatherService;
        }

        /// <summary>
        /// 根据城市编码获取天气信息
        /// </summary>
        /// <param name="cityCode">城市编码，如：110000（北京）、310000（上海）</param>
        /// <param name="extensions">返回结果类型：base-返回实况天气，all-返回预报天气</param>
        /// <returns>天气信息</returns>
        [HttpGet("city/{cityCode}")]
        public async Task<IActionResult> GetWeatherByCityCode(string cityCode, [FromQuery] string extensions = "base")
        {
            try
            {
                var result = await _weatherService.GetWeatherByCityCodeAsync(cityCode, extensions);
                return Ok(result);
            }
            catch (ArgumentException ex)
            {
                return BadRequest(new { error = ex.Message });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { error = ex.Message });
            }
        }

        /// <summary>
        /// 根据经纬度获取天气信息
        /// </summary>
        /// <param name="longitude">经度</param>
        /// <param name="latitude">纬度</param>
        /// <param name="extensions">返回结果类型：base-返回实况天气，all-返回预报天气</param>
        /// <returns>天气信息</returns>
        [HttpGet("location")]
        public async Task<IActionResult> GetWeatherByLocation([FromQuery] double longitude, [FromQuery] double latitude, [FromQuery] string extensions = "base")
        {
            try
            {
                // 验证经纬度范围
                if (longitude < -180 || longitude > 180)
                {
                    return BadRequest(new { error = "经度必须在-180到180之间" });
                }
                if (latitude < -90 || latitude > 90)
                {
                    return BadRequest(new { error = "纬度必须在-90到90之间" });
                }

                var result = await _weatherService.GetWeatherByLocationAsync(longitude, latitude, extensions);
                return Ok(result);
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { error = ex.Message });
            }
        }

        /// <summary>
        /// 根据经纬度获取城市编码
        /// </summary>
        /// <param name="longitude">经度</param>
        /// <param name="latitude">纬度</param>
        /// <returns>城市编码</returns>
        [HttpGet("citycode")]
        public async Task<IActionResult> GetCityCodeByLocation([FromQuery] double longitude, [FromQuery] double latitude)
        {
            try
            {
                // 验证经纬度范围
                if (longitude < -180 || longitude > 180)
                {
                    return BadRequest(new { error = "经度必须在-180到180之间" });
                }
                if (latitude < -90 || latitude > 90)
                {
                    return BadRequest(new { error = "纬度必须在-90到90之间" });
                }

                var cityCode = await _weatherService.GetCityCodeByLocationAsync(longitude, latitude);
                return Ok(new { cityCode });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { error = ex.Message });
            }
        }
    }
}

